<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">添加新闻评论</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
        <el-select v-model="searchParam.item_type" placeholder="请选择点赞分类" class="user-input">
          <el-option v-for="item in item_typeOptions"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-input v-model="searchParam.item_id" placeholder="请输入新闻编号" class="user-input"></el-input>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="点赞分类" prop="item_type"></el-table-column>
        <el-table-column label="新闻编号" prop="item_id"></el-table-column>
        <el-table-column label="IP地址" prop="ip"></el-table-column>
        <el-table-column label="添加时间" prop="add_time"></el-table-column>
        <el-table-column label="评论内容" prop="content"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="添加新闻评论" :visible.sync="showAddModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>点赞分类：</label>
        <div class="input-group-box"><el-input v-model="addParam.item_type" placeholder="请输入点赞分类"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>新闻编号：</label>
        <div class="input-group-box"><el-input v-model="addParam.item_id" placeholder="请输入新闻编号"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>IP地址：</label>
        <div class="input-group-box"><el-input v-model="addParam.ip" placeholder="请输入IP地址"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>评论内容：</label>
        <div class="input-group-box"><el-input v-model="addParam.content" placeholder="请输入评论内容"></el-input></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改新闻评论" :visible.sync="showUpdateModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>点赞分类：</label>
        <div class="input-group-box"><el-input v-model="editParam.item_type" placeholder="请输入点赞分类"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>新闻编号：</label>
        <div class="input-group-box"><el-input v-model="editParam.item_id" placeholder="请输入新闻编号"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>IP地址：</label>
        <div class="input-group-box"><el-input v-model="editParam.ip" placeholder="请输入IP地址"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>评论内容：</label>
        <div class="input-group-box"><el-input v-model="editParam.content" placeholder="请输入评论内容"></el-input></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import apiConfig from '../../utils/apiconfig'
  import api from '../../utils/api'
  import helper from '../../utils/helper'
  import loading from '../../components/loading'
  export default {
    data () {
      return {
        listData: [],
        pageSize: 20,
        pageall: 1,
        page: 1,
        loading: true,
        showAddModelFlag: false,
        showUpdateModelFlag: false,
        searchParam: {
          id: '',
          item_type: '',
          item_id: ''
        },
        addParam: {
          item_type: '',
          item_id: '',
          ip: '',
          content: ''
        },
        editParam: {
          id: '',
          item_type: '',
          item_id: '',
          ip: '',
          content: ''
        }
      }
    },
    created () {
      this.getList()
    },
    components: {
      loading
    },
    methods: {
      getSearchParam () { // 参数
        var param = {
          page: this.page,
          id: this.searchParam.id,
          item_type: this.searchParam.item_type,
          item_id: this.searchParam.item_id
        }
        this.params = param
      },
      getList () {
        this.getSearchParam()
        api.GET(helper.getManageApiUrl(apiConfig.newsCommentList), this.params, res => {
          this.listData = res
          this.loading = false
        }, res => {
          console.log(res)
        })
      },
      search () {
        this.getList()
      },
      cancel () {
        this.searchParam.item_type = ''
        this.searchParam.item_id = ''
        this.searchParam.ip = ''
        this.searchParam.content = ''
      },
      handleChange (val) {
        this.page = val
        this.getList()
      },
      add () {
        this.showAddModelFlag = true
      },
      confirmAdd () {
        var param = {
          item_type: this.addParam.item_type,
          item_id: this.addParam.item_id,
          ip: this.addParam.ip,
          content: this.addParam.content
        }
        api.GET(helper.getManageApiUrl(apiConfig.addNewsComment), param, res => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 1500
          })
          this.addParam.item_type = ''
          this.addParam.item_id = ''
          this.addParam.ip = ''
          this.addParam.content = ''
          this.showAddModelFlag = false
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: res.msg,
            type: 'fail',
            duration: 1500
          })
        })
      },
      remove (id) {
        var param = {
          id: id
        }
        this.$confirm('确认要删除此条信息吗？')
          .then(_ => {
            api.GET(helper.getManageApiUrl(apiConfig.deleteNewsComment), param, res => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 1500
              })
              this.getList()
            }, res => {
              this.$notify({
                title: '失败',
                message: '删除失败',
                type: 'error',
                duration: 1500
              })
            })
          })
          .catch(_ => {})
      },
      edit (item) {
        this.editParam.id = item.id
        this.editParam.item_type = item.item_type
        this.editParam.item_id = item.item_id
        this.editParam.ip = item.ip
        this.editParam.content = item.content
        this.showUpdateModelFlag = true
      },
      save () {
        api.GET(helper.getManageApiUrl(apiConfig.updateNewsComment), this.editParam, res => {
          this.$notify({
            title: '成功',
            message: '修改成功',
            type: 'success',
            duration: 1500
          })
          this.showUpdateModelFlag = false
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '修改失败',
            type: 'error',
            duration: 1500
          })
        })
      }
    }
  }
</script>
<style>
  .search-box {
    font-size: 0;
    padding-bottom:10px;
  }
  .search-box .user-input{
    width: 197px;
    margin: 3px 2px;
  }
</style>

